  /**
 * @file   USB2XXX_CAN_UDSTest.cpp
 * @brief  CAN UDS测试程序，该程序可以适用于普通CAN或者CANFD
 * @author wdluo(wdluo@toomoss.com)
 * @version 1.0
 * @date 2022-11-18
 * @copyright Copyright (c) 2022 重庆图莫斯电子科技有限公司
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "usb_device.h"
#include "usb2can.h"
#include "usb2canfd.h"
#include "can_uds.h"

#if _WIN32
#include <windows.h>
#pragma comment(lib,"USB2XXX.lib")
#endif

#define GET_FIRMWARE_INFO   1

int main(int argc, const char* argv[])
{
#if GET_FIRMWARE_INFO
    DEVICE_INFO DevInfo;
#endif
    int DevHandle[10];
    int CANIndex = 0;
    int DEVIndex = 0;
    bool state;
    int ret;
    //扫描查找设备
    ret = USB_ScanDevice(DevHandle);
    if(ret <= 0){
        printf("No device connected!\r\n");
        return 0;
    }
    //打开设备
    state = USB_OpenDevice(DevHandle[DEVIndex]);
    if(!state){
        printf("Open device error!\r\n");
        return 0;
    }
#if GET_FIRMWARE_INFO
    char FunctionStr[256]={0};
    //获取固件信息
    state = DEV_GetDeviceInfo(DevHandle[DEVIndex],&DevInfo,FunctionStr);
    if(!state){
        printf("Get device infomation error!\r\n");
        return 0;
    }else{
        printf("Firmware Info:\r\n");
	    printf("Firmware Name:%s\r\n",DevInfo.FirmwareName);
        printf("Firmware Build Date:%s\r\n",DevInfo.BuildDate);
        printf("Firmware Version:v%d.%d.%d\r\n",(DevInfo.FirmwareVersion>>24)&0xFF,(DevInfo.FirmwareVersion>>16)&0xFF,DevInfo.FirmwareVersion&0xFFFF);
        printf("Hardware Version:v%d.%d.%d\r\n",(DevInfo.HardwareVersion>>24)&0xFF,(DevInfo.HardwareVersion>>16)&0xFF,DevInfo.HardwareVersion&0xFFFF);
	    printf("Firmware Functions:%s\r\n",FunctionStr);
    }
#endif
    if(((DevHandle[DEVIndex]&0xFF000000)==0x53000000)||((DevHandle[DEVIndex]&0xFF000000)==0x54000000)){
        CANFD_INIT_CONFIG CANFDConfig;
        ret = CANFD_GetCANSpeedArg(DevHandle[DEVIndex],&CANFDConfig,500000,2000000); //获取CANFD波特率参数  
        if(ret == CANFD_SUCCESS){
            CANFDConfig.ResEnable = 1;//接入终端电阻
            ret = CANFD_Init(DevHandle[DEVIndex],CANIndex,&CANFDConfig);
            if(ret != CANFD_SUCCESS){
                printf("CANFD Init Error!\n");
                return 0;
            }else{
                printf("CANFD Init Success!\n");
            }
            CANFD_StartGetMsg(DevHandle[DEVIndex],CANIndex);
        }else{
            printf("CANFD Get speed  arguments failed!\n");
            return 0;
        }
    }else{
        //初始化配置CAN
        CAN_INIT_CONFIG CANConfig;
        ret = CAN_GetCANSpeedArg(DevHandle[DEVIndex],&CANConfig,500000);  //获取CAN波特率参数  
        if(ret == CAN_SUCCESS){
            CANConfig.CAN_Mode = 0x80;//正常模式，接入终端电阻
            ret = CAN_Init(DevHandle[DEVIndex],CANIndex,&CANConfig);
            if(ret != CAN_SUCCESS){
                printf("Config CAN failed!\r\n");
                return 0;
            }else{
                printf("Config CAN Success!\r\n");
            }
            CAN_StartGetMsg(DevHandle[DEVIndex],CANIndex);
        }else{
            printf("CAN Get speed  arguments failed!\n");
            return 0;
        }
    }
    printf("Begin CAN UDS Slave Test!DevHandle=0x%08X\n",DevHandle[DEVIndex]);
    //模拟单片机程序的死循环
    while(1){
        if(((DevHandle[DEVIndex]&0xFF000000)==0x53000000)||((DevHandle[DEVIndex]&0xFF000000)==0x54000000)){
            CANFD_MSG Msg[1000];
            int ret = CANFD_GetMsg(DevHandle[DEVIndex],CANIndex,Msg,1000);
            for(int i=0;i<ret;i++){
                if(((Msg[i].ID)==(PHY_ADDR_ID|(MSG_ID_TYPE<<31)))||((Msg[i].ID)==(FUN_ADDR_ID|(MSG_ID_TYPE<<31)))){
                    CAN_UDS_Process(DevHandle[DEVIndex],CANIndex,Msg[i].Data,Msg[i].DLC);
                }
            }
        }else{
            CAN_MSG Msg[1000];
            int ret = CAN_GetMsgWithSize(DevHandle[DEVIndex],CANIndex,Msg,1000);
            for(int i=0;i<ret;i++){
                if(((Msg[i].ID==PHY_ADDR_ID)&&(MSG_ID_TYPE==(Msg[i].ExternFlag&0x01)))||((Msg[i].ID==FUN_ADDR_ID)&&(MSG_ID_TYPE==(Msg[i].ExternFlag&0x01)))){
                    CAN_UDS_Process(DevHandle[DEVIndex],CANIndex,Msg[i].Data,Msg[i].DataLen);
                }
            }
        }
#if _WIN32
        Sleep(1);
#else
        usleep(1*1000);
#endif
    }
	return 0;
}

